<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">

    <link href="../../../jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>
    <link href="../../../jquery/bootstrap-datetimepicker-master/css/bootstrap-datetimepicker.min.css" type="text/css"
          rel="stylesheet"/>

    <script type="text/javascript" src="../../../jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="../../../jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>

</head>
<body>

<div style="position:  relative; left: 30px;">
    <h3>修改字典类型</h3>
    <div style="position: relative; top: -40px; left: 70%;">
        <button type="button" id="btnEdit" class="btn btn-primary">更新</button>
        <button type="button" class="btn btn-default" onclick="window.history.back();">取消</button>
    </div>
    <hr style="position: relative; top: -40px;">
</div>
<form class="form-horizontal" role="form">

    <div class="form-group">
        <label for="create-code" class="col-sm-2 control-label">编码<span
                style="font-size: 15px; color: red;">*</span></label>
        <div class="col-sm-10" style="width: 300px;">
            <input type="text" readonly class="form-control" id="code" style="width: 200%;" placeholder="编码作为主键，不能是中文"
                   value="sex">
        </div>
    </div>

    <div class="form-group">
        <label for="create-name" class="col-sm-2 control-label">名称</label>
        <div class="col-sm-10" style="width: 300px;">
            <input type="text" class="form-control" id="name" style="width: 200%;" value="性别">
        </div>
    </div>

    <div class="form-group">
        <label for="create-describe" class="col-sm-2 control-label">描述</label>
        <div class="col-sm-10" style="width: 300px;">
            <textarea class="form-control" rows="3" id="describe" style="width: 200%;">描述信息</textarea>
        </div>
    </div>
</form>

<div style="height: 200px;"></div>
</body>
</html>

<script>

    //前端获取url网址上面的参数值
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]);
        return null; //返回参数值
    }
    $(function () {
        //判定是否有id传递过来
        var urlParam = getUrlParam("id");
        if (urlParam === null) {
            console.log("code is null");
            alert("编辑的code没有提供，编辑错误");
            window.location = "index.html";
            return;
        }



        //编辑可以获取编辑之前的数据
        $.ajax({
            url: "/DictionaryType/editBefore.action",
            type: "post",
            dataType: "json",
            data: {
                id: urlParam
            },
            beforeSend: function (XMLHttpRequest) {
                console.log("beforeSend");
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log("error");
                console.log("textStatus:" + textStatus);
                console.log("errorThrown:" + errorThrown);
            },
            success: function (data, textStatus, jqXHR) {
                console.log("success");
                console.log(data.code);
                console.log(data.name);
                console.log(data.description);

                $("#code").val(data.code);
                $("#name").val(data.name);
                $("#describe").val(data.description);

            },
            complete: function (XMLHttpRequest, textStatus) {
                console.log("complete");
            }
        });

        $("#btnEdit").click(function () {

            $.ajax({
                url: "/DictionaryType/edit.action",
                type: "post",
                dataType: "json",
                data: {
                    code:$("#code").val(),
                    name:$("#name").val(),
                    description:$("#describe").val()
                },
                beforeSend: function (XMLHttpRequest) {

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {

                },
                success: function (data, textStatus, jqXHR) {

                    if(data['result']=="success"){
                        window.location.href='index.html';
                    }
                    else{
                        window.alert(data['mess']);
                    }
                },
                complete: function (XMLHttpRequest, textStatus) {
                }
            });
        });


    });


</script>